package com.megan.common.core.response;

import com.megan.common.core.enums.ResponseCode;
import com.megan.common.core.exception.BizException;
import lombok.Data;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@Data
public class ServerResponse<T> implements Serializable {
    private static final long serialVersionUID = -4543918147603748302L;

    private int responseCode;
    private String responseMsg;
    private transient T responseData;

    public ServerResponse() {
    }

    private ServerResponse(int responseCode) {
        this.responseCode = responseCode;
    }

    private ServerResponse(int responseCode, T responseData) {
        this.responseCode = responseCode;
        this.responseData = responseData;
    }

    private ServerResponse(int responseCode, String responseMsg, T responseData) {
        this.responseCode = responseCode;
        this.responseMsg = responseMsg;
        this.responseData = responseData;
    }

    private ServerResponse(int responseCode, String responseMsg) {
        this.responseCode = responseCode;
        this.responseMsg = responseMsg;
    }

    @JsonIgnore
    //使之不在json序列化结果当中
    public boolean isSuccess() {
        return this.responseCode == ResponseCode.SUCCESS.getCode();
    }

    public int getResponseCode() {
        return responseCode;
    }

    public T getResponseData() {
        return responseData;
    }

    public String getResponseMsg() {
        return responseMsg;
    }


    public static <T> ServerResponse<T> createBySuccess() {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String responseMsg) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), responseMsg);
    }

    public static <T> ServerResponse<T> createBySuccess(T responseData) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), responseData);
    }

    public static <T> ServerResponse<T> createBySuccess(String responseMsg, T responseData) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), responseMsg, responseData);
    }


    public static <T> ServerResponse<T> createByError() {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg());
    }

    public static <T> ServerResponse<T> createByError(BizException e) {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), e.getMessage());
    }

    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new ServerResponse<T>(errorCode, errorMessage);
    }

    public static <T> ServerResponse<T> createByErrorEnum(ResponseCode errorCode) {
        return new ServerResponse<T>(errorCode.getCode(), errorCode.getMsg());
    }
}